/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ajustecurvas.view;

import ajustecurvas.Ponto;
import java.awt.Color;
import java.awt.Shape;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import javax.swing.JPanel;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.labels.StandardCategoryToolTipGenerator;
import org.jfree.chart.labels.StandardXYItemLabelGenerator;
import org.jfree.chart.labels.StandardXYSeriesLabelGenerator;
import org.jfree.chart.labels.StandardXYToolTipGenerator;
import org.jfree.chart.labels.XYSeriesLabelGenerator;
import org.jfree.chart.labels.XYToolTipGenerator;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

/**
 *
 * @author positivo
 */
public class GraficoComplexo {

    JPanel container;
    private ChartPanel chartPanel;

    private int contadorSerie = 0;

    // Create a single plot containing both the scatter and line
    XYPlot plot = new XYPlot();

    protected GraficoComplexo(JPanel container)
    {
        this.container = container;
        initChartPanel();
    }

    private void initChartPanel()
    {
        this.chartPanel = new ChartPanel(getDefaultChart());
        chartPanel.setSize(container.getWidth(),container.getHeight());
        chartPanel.setVisible(true);
        container.add(chartPanel);
    }

    private JFreeChart getDefaultChart()
    {
        XYDataset collection1 = new XYSeriesCollection();
        XYItemRenderer renderer1 = new XYLineAndShapeRenderer(false, true);   // Shapes only
        ValueAxis domain1 = new NumberAxis("x");
        ValueAxis range1 = new NumberAxis("f(x)");
        

        // Set the scatter data, renderer, and axis into plot
        plot.setDataset(0, collection1);
        plot.setRenderer(0, renderer1);
        plot.setDomainAxis(0, domain1);
        plot.setRangeAxis(0, range1);

        // Map the scatter to the first Domain and first Range
        plot.mapDatasetToDomainAxis(0, 0);
        plot.mapDatasetToRangeAxis(0, 0);

        JFreeChart chart = new JFreeChart("Ajuste de Pontos", JFreeChart.DEFAULT_TITLE_FONT, plot, true);

        return chart;
    }

    public void addCurva(List<Ponto> pontos, String nome)
    {

        // Create the line data, renderer, and axis
        XYDataset collection2 = createDataset(pontos, nome);
        XYItemRenderer renderer2 = new XYLineAndShapeRenderer(true, false);   // Lines only

        renderer2.setSeriesPaint( 0, new Color( 0, 0, 255 ) );
        

        // Set the line data, renderer, and axis into plot
        plot.setDataset(2, collection2);
        plot.setRenderer(2, renderer2);
        
        // Map the line to the first Domain and first Range
        plot.mapDatasetToDomainAxis(2, 0);
        plot.mapDatasetToRangeAxis(2, 0);

    }

    public void addDispersao(List<Ponto> pontos)
    {
        // Create the line data, renderer, and axis
        XYDataset collection2 = createDataset(pontos, "dispersão");
        XYItemRenderer renderer2 = new XYLineAndShapeRenderer(false, true);   // Shapes only

        renderer2.setSeriesPaint( 0, Color.RED );

        //renderer2.setSeriesShape(0, new Rectangle2D.Double());

        // Set the line data, renderer, and axis into plot
        plot.setDataset(1, collection2);
        plot.setRenderer(1, renderer2);

        // Map the line to the first Domain and first Range
        plot.mapDatasetToDomainAxis(1, 0);
        plot.mapDatasetToRangeAxis(1, 0);

    }

    public void reinicializar()
    {
        int total = plot.getDatasetCount();
        for (int i = 0; i < total; i++) {
            plot.setDataset(i, null);
        }
    }

    public void redimensionar()
    {
        chartPanel.setSize(container.getWidth(),container.getHeight());
    }

    private XYDataset createDataset(List<Ponto> pontos, String nome)
    {
        XYSeries serie = new XYSeries (nome);
        contadorSerie++;

        for (Ponto ponto : pontos) {
            serie.add(ponto.getX(), ponto.getY());
        }

        XYSeriesCollection c = new XYSeriesCollection(serie);
        return c;

    }

    //CÓDIGO NO QUAL ME BASEEI

//    private JFreeChart getChart()
//    {
//
//
//
//        /* SETUP SCATTER */
//
//        // Create the scatter data, renderer, and axis
//        XYDataset collection1 = createDataset1();
//        XYItemRenderer renderer1 = new XYLineAndShapeRenderer(false, true);   // Shapes only
//        ValueAxis domain1 = new NumberAxis("Domain1");
//        ValueAxis range1 = new NumberAxis("Range1");
//
//        // Set the scatter data, renderer, and axis into plot
//        plot.setDataset(0, collection1);
//        plot.setRenderer(0, renderer1);
//        plot.setDomainAxis(0, domain1);
//        plot.setRangeAxis(0, range1);
//
//        // Map the scatter to the first Domain and first Range
//        plot.mapDatasetToDomainAxis(0, 0);
//        plot.mapDatasetToRangeAxis(0, 0);
//
//        /* SETUP LINE */
//
//        // Create the line data, renderer, and axis
//        XYDataset collection2 = createDataset2();
//        XYItemRenderer renderer2 = new XYLineAndShapeRenderer(true, false);   // Lines only
//        ValueAxis domain2 = new NumberAxis("Domain2");
//        ValueAxis range2 = new NumberAxis("Range2");
//
//        // Set the line data, renderer, and axis into plot
//        plot.setDataset(1, collection2);
//        plot.setRenderer(1, renderer2);
//        plot.setDomainAxis(1, domain2);
//        plot.setRangeAxis(1, range2);
//
//        // Map the line to the second Domain and second Range
//        plot.mapDatasetToDomainAxis(1, 1);
//        plot.mapDatasetToRangeAxis(1, 1);
//
//        // Create the chart with the plot and a legend
//        JFreeChart chart = new JFreeChart("Multi Dataset Chart", JFreeChart.DEFAULT_TITLE_FONT, plot, true);
//
//        return chart;
//    }
//
//    private XYDataset createDataset1()
//    {
//
//        List<Ponto> pontos = new ArrayList<Ponto>();
//        pontos.add(new Ponto(1.0,0.0));
//        pontos.add(new Ponto(1.5,0.0));
//        pontos.add(new Ponto(2.0,0.0));
//        pontos.add(new Ponto(2.5,0.0));
//        pontos.add(new Ponto(3.0,0.0));
//        pontos.add(new Ponto(3.5,0.0));
//        pontos.add(new Ponto(4.0,0.0));
//        pontos.add(new Ponto(4.5,0.0));
//        pontos.add(new Ponto(5.0,0.0));
//        pontos.add(new Ponto(5.5,0.0));
//        pontos.add(new Ponto(6.0,0.0));
//
//
//        XYSeries serie = new XYSeries ("Data");
//
//        for (Ponto ponto : pontos) {
//            serie.add(ponto.getX(), ponto.getY());
//        }
//
//        XYSeriesCollection c = new XYSeriesCollection(serie);
//        return c;
//    }
//
//    private XYDataset createDataset2()
//    {
//
//        List<Ponto> pontos = new ArrayList<Ponto>();
//        pontos.add(new Ponto(1.0,0.5250));
//        pontos.add(new Ponto(1.5,0.7448));
//        pontos.add(new Ponto(2.0,1.2807));
//        pontos.add(new Ponto(2.5,1.9934));
//        pontos.add(new Ponto(3.0,2.5326));
//        pontos.add(new Ponto(3.5,3.5386));
//        pontos.add(new Ponto(4.0,5.4400));
//        pontos.add(new Ponto(4.5,6.6258));
//        pontos.add(new Ponto(5.0,9.7768));
//        pontos.add(new Ponto(5.5,11.5076));
//        pontos.add(new Ponto(6.0,14.9484));
//
//
//        XYSeries serie = new XYSeries ("Data");
//
//        for (Ponto ponto : pontos) {
//            serie.add(ponto.getX(), ponto.getY());
//        }
//
//        XYSeriesCollection c = new XYSeriesCollection(serie);
//        return c;
//    }   

}
